package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.SySwitchMapper;
import com.eco.fanliapp.entity.SySwitch;
import com.eco.fanliapp.service.ISySwitchService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 开关表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-08-28
 */
@Service
public class SySwitchServiceImpl extends ServiceImpl<SySwitchMapper, SySwitch> implements ISySwitchService {

	@Autowired
	private SySwitchMapper sySwitchMapper;

	@Override
	public ServerResponse findSwitchList(Integer switchId, String switchName) {
		QueryWrapper<SySwitch> query = new QueryWrapper<>();
		if(switchId != null){
			query.eq("table_id", switchId);
		}
		if(switchName != null && !"".equals(switchName)){
			query.like("switch_name", switchName);
		}
		query.eq("enable",true);
		query.eq("deleted",false);
		List<SySwitch> list = list(query);
		return ServerResponse.createBySuccess("返回开关列表",list);
	}

	@Override
	public ServerResponse createSwitch(Integer platform, String switchName, Integer state,String switchDesc) {
		if(switchName != null && "".equals(switchName)){
			return ServerResponse.createByErrorMessage("switchName不能为空！");
		}
		//不允许重复重建
		QueryWrapper<SySwitch> query = new QueryWrapper<>();
		query.eq("switch_name",switchName);
		query.eq("deleted",false);
		List<SySwitch> list = list(query);
		if(CollectionUtils.isNotEmpty(list)){
			return ServerResponse.createByErrorMessage("名称不允许重复");
		}
		//保存
		SySwitch st = new SySwitch();
		if(platform != null){
			st.setPlatform(platform);
		}
		st.setSwitchName(switchName);
		st.setSwitchDesc(switchDesc);
		if(state != null){
			st.setState(state);
		}
		save(st);
		return ServerResponse.createBySuccessMessage("新增开关成功！");
	}

	@Override
	public ServerResponse updateOrDelSwitch(Integer switchId, Integer platform, String switchName, String switchDesc,Integer state, Integer enable, Integer deleted) {
		if(switchId == null){
			return  ServerResponse.createByErrorMessage("switchId不能为空！");
		}
		SySwitch sySwitch = getById(switchId);
		if(sySwitch == null){
			return ServerResponse.createByErrorMessage("开关不存在");
		}
		//更改属性值
		if(platform != null){
			sySwitch.setPlatform(platform);
		}
		if(switchName != null && !"".equals(switchName)){
			sySwitch.setSwitchName(switchName);
		}
		if(switchDesc != null && !"".equals(switchDesc)){
			sySwitch.setSwitchDesc(switchDesc);
		}
		if(state != null){
			sySwitch.setState(state);
		}
		if(enable != null){
			sySwitch.setEnable(enable == 1?true:false);
		}
		if(deleted != null){
			sySwitch.setDeleted(deleted == 1?true:false);
		}
		updateById(sySwitch);
		return ServerResponse.createBySuccessMessage("操作成功！！");
	}

	@Override
	public ServerResponse switchOffUpdate(Integer switchId, Integer state) {
		HashMap map = new HashMap();
		SySwitch sySwitch = new SySwitch();
		sySwitch.setTableId(switchId.longValue());
		sySwitch.setState(state);
		int i = sySwitchMapper.updateById(sySwitch);
		if(i>0){
			SySwitch sySwitchs = sySwitchMapper.selectById(switchId);
			map.put("switchId",sySwitchs.getTableId());
			map.put("state",sySwitchs.getState());
			return ServerResponse.createBySuccess("操作成功",map);
		}else {
			SySwitch sySwitche = sySwitchMapper.selectById(switchId);
			map.put("switchId",sySwitche.getTableId());
			map.put("state",sySwitche.getState());
			return ServerResponse.createByError("操作失败",map);
		}
	}

	@Override
	public ServerResponse switchOffList() {
		QueryWrapper<SySwitch> query = new QueryWrapper<>();
		query.eq("deleted",false);
		List<SySwitch> list = sySwitchMapper.selectList(query);
		if (list != null) {
			return ServerResponse.createBySuccess("查询成功",list);
		}else {
			return ServerResponse.createBySuccess(201,"暂无数据",null);
		}
	}
}
